package com.bone.workstation.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.bone.com.service.ComLogService;
import com.bone.constant.Constant;
import com.bone.page.Page;
import com.bone.vo.EmployeeRoleVO;
import com.bone.vo.ModuleMenu;
import com.bone.vo.RoleModuleVO;
import com.bone.workstation.dao.EmployeeDAO;
import com.bone.workstation.dao.EmployeeRoleDAO;
import com.bone.workstation.dao.ModuleDAO;
import com.bone.workstation.dao.RoleDAO;
import com.bone.workstation.dao.RoleModuleDAO;
import com.bone.workstation.po.Employee;
import com.bone.workstation.po.Module;
import com.bone.workstation.po.Role;
import com.bone.workstation.service.ApplicationService;

public class ApplicationServiceImpl implements  ApplicationService{
	
	
	/**
	 * 角色DAO.
	 */
	private RoleDAO roleDAO;

	/**
	 * 角色模板DAO. 
	 */
	private RoleModuleDAO roleModuleDAO;
	/**
	 * 模板DAO.
	 */
	private ModuleDAO moduleDAO;
	
	/**
	 * DAO.
	 */
	private EmployeeDAO employeeDAO;
	/**
	 * 	
	 */
	private ComLogService comLogService;
	
	/**
	 * employeeRoleDAO.
	 */
	private EmployeeRoleDAO employeeRoleDAO;
	 
	
	private static int INTERVAL = 4;

	/**
	 * 该用户的角色模块.
	 * @param user
	 * @return
	 */
	@Override
	public List<ModuleMenu> findMyModuleList(Long employeeId) {
		List<ModuleMenu> ModuleMenus = new ArrayList<ModuleMenu>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("employeeId", employeeId.toString());
		map.put("roleStatus", Constant.COMMON_STATUS.NORMAL.toString());
		map.put("moduleStatus", Constant.COMMON_STATUS.NORMAL.toString());
		List<Module> moduleList = this.moduleDAO.queryValidModuleByemployeeId(map);
		for (Module Module : moduleList) {
			if (Module.getModuleCode().length() == INTERVAL ) {
				ModuleMenu mm = new ModuleMenu();
				mm.setModule(Module);
				ModuleMenus.add(mm);
			} else if (Module.getModuleCode().length() == INTERVAL * 2 ) {
				for (ModuleMenu mm : ModuleMenus) {
					if (Module.getModuleCode().startsWith(
							mm.getModule().getModuleCode())) {
						ModuleMenu childMenu = new ModuleMenu();
						childMenu.setModule(Module);
						mm.getChildModules().add(childMenu);
					}
				}
			}
		}
		return ModuleMenus;
	}
	
	/**
	 * 取属于该角色的模块.
	 * @param parameter
	 * @return
	 */
	@Override
	public List<RoleModuleVO> queryRoleModuleVoListByParam(Map<String, String> map){
		return this.roleModuleDAO.queryRoleModuleVoListByParam(map);
	}
	
//-------------role --------------------------------------------------------	
	/**
	 * 取符合条件的模块的数量.
	 * @param para
	 * @return
	 */
	@Override
	public Long queryRoleCountByParam(Map<String, Object> para){
		return roleDAO.queryRoleCountByParam(para);
		
	}
	
	/**
	 * 取符合条件的模块的LIST.
	 * @param para
	 * @return
	 */
	@Override
	public List<Role> queryRolePageByParam(Map<String, Object> para){
		return roleDAO.queryRolePageByParam(para);
	}
	
	/**
	 * 角色数据列表(List).
	 * @param role
	 * @return
	 */
	@Override
	public List<Role> queryRoleListByParam(Role role) {
		return this.roleDAO.queryRoleListByParam(role);
	}
	
	/**
	 * 根据角色的ID取角色的信息 .
	 * @param parameter
	 * @return
	 */
	@Override
	public Role queryRoleByRoleId(Long roleId){
		return roleDAO.queryRoleByRoleId(roleId);
	}
	
	/**
	 * 保存或者修改角色.	
	 * @param module
	 * @return
	 */
	@Override
	public boolean saveOrUpdateRole(Role role,String operatorId){
		boolean isSuccess = false;
		String type ="";
		if(role.getRoleId()!=null && role.getRoleId() > 0){
			isSuccess = roleDAO.updateRole(role);
			type = "UPDATE";
		}else{
			role.setCreateTime(new Date());
			role.setOperator(String.valueOf(operatorId));
			isSuccess = roleDAO.insertRole(role);
			type = "SAVE";
		}
		if("SAVE".equals(type)){
			comLogService.insertComlog("SYS_MODULE", role.getRoleId(), operatorId,"SAVE", "添加角色","添加角色","");
		}if("UPDATE".equals(type)){
			comLogService.insertComlog("SYS_MODULE", role.getRoleId(), operatorId,"UPDATE", "更新角色","更新角色","");
		}
		return isSuccess;
		
		
	} 
	 
	//-------module--------------------------------------
	
	/**
	 * 取符合条件的模块的数量.
	 * @param para
	 * @return
	 */
	@Override
	public Long queryModuleCountByParam(Map<String, Object> para){
		return moduleDAO.queryModuleCountByParam(para);
		
	}
	/**
	 * 取符合条件的模块的LIST.
	 * @param para
	 * @return
	 */
	@Override
	public List<Module> queryModuleListByParam(Map<String, Object> para){
		para.put("_order", "MODULE_CODE");
		return moduleDAO.queryModuleListByParam(para);
	}
	
	/**
	 * 根据模块的ID取模块的信息 .
	 * @param parameter
	 * @return
	 */
	@Override
	public Module queryModuleByModuleId(Long moduleId){
		return this.moduleDAO.queryModuleByModuleId(moduleId);
	}
	
	/**
	 * 保存或者修改模块.	
	 * @param module
	 * @return
	 */
	@Override
	public boolean saveOrUpdateModule(Module module,String operatorId){
		boolean isSuccess = false;
		String type ="";
		if(module.getModuleId()!=null && module.getModuleId() > 0){
			isSuccess = moduleDAO.updateModule(module);
			type = "UPDATE";
		}else{
			module.setModuleType("0");
			module.setCreateTime(new Date());
			module.setOperator(String.valueOf(operatorId));
			isSuccess = moduleDAO.insertModule(module);
			type = "SAVE";
		}
		if("SAVE".equals(type)){
			comLogService.insertComlog("SYS_MODULE", module.getModuleId(), operatorId,"SAVE", "添加模块","添加模块","");
		}if("UPDATE".equals(type)){
			comLogService.insertComlog("SYS_MODULE", module.getModuleId(), operatorId,"UPDATE", "更新模块","更新模块","");
		}
		return isSuccess;
	} 
	
	
	
	/**
	 * 根据登录名和密码查询用户.
	 * @param account
	 * @param password
	 * @return
	 */
	@Override
	public Employee queryEmployeeByParam(Employee Employee) {
		return this.employeeDAO.queryEmployeeByParam(Employee);
	}

	/**
	 * 取符合条件的后台用户数据的数量.
	 * @param para
	 * @return
	 */
	@Override
	public Long queryEmployeeCountByParam(Map<String, Object> para){
		return employeeDAO.queryEmployeeCountByParam(para);
		
	}
	/**
	 * 取符合条件的后台用户数据的LIST.
	 * @param para
	 * @return
	 */
	@Override
	public List<Employee> queryEmployeePageByParam(Map<String, Object> para){
		para.put("_order", "employee_id");
		return employeeDAO.queryEmployeePageByParam(para);
	}
	
	/**
	 * 取用户的角色列表数据.
	 * @param parameter
	 * @return
	 */
	public List<Employee> queryEmployeeListByParam(Map<String, Object> para){
		para.put("_order", "employee_id");
		return employeeDAO.queryEmployeeListByParam(para);
	}
	/**
	 *  按PK取Employee.
	 * @param parameter
	 * @return
	 */
	@Override
	public Employee queryByEmployeeId(Long employeeId){
		return  this.employeeDAO.queryByEmployeeId(employeeId);
	}
	/**
	 * 保存/修改雇员信息.
	 * @param employee
	 * @return
	 */
	@Override
	public boolean saveOrUpdateEmployee(Employee employee){
		if (employee.getEmployeeId() == null) {
			employee.setCreateTime(new Date());
		}
		employeeDAO.saveOrUpdateEmployee(employee);
		return true;
	}
	
	/**
	 * 取该用户的角色.
	 * @param employeeId
	 * @return
	 */
	@Override
	public List<EmployeeRoleVO> queryEmployeeRoleByEmployeeId(Long employeeId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("employeeId",employeeId);
		return this.employeeRoleDAO.queryEmployeeRoleVoByParam(map);
	}
	
	


	public RoleDAO getRoleDAO() {
		return roleDAO;
	}
	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	public RoleModuleDAO getRoleModuleDAO() {
		return roleModuleDAO;
	}
	public void setRoleModuleDAO(RoleModuleDAO roleModuleDAO) {
		this.roleModuleDAO = roleModuleDAO;
	}
	public ModuleDAO getModuleDAO() {
		return moduleDAO;
	}
	public void setModuleDAO(ModuleDAO moduleDAO) {
		this.moduleDAO = moduleDAO;
	}
	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}
	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}
	public EmployeeRoleDAO getEmployeeRoleDAO() {
		return employeeRoleDAO;
	}
	public void setEmployeeRoleDAO(EmployeeRoleDAO employeeRoleDAO) {
		this.employeeRoleDAO = employeeRoleDAO;
	}
	public void setComLogService(ComLogService comLogService) {
		this.comLogService = comLogService;
	}
	public ComLogService getComLogService() {
		return comLogService;
	}
 
}
